package com.ymatou.iapi.login.testcase;

import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.login.parameter.WithoutpwdloginBean;
import com.ymatou.iapi.login.service.WithoutpwdloginCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.enums.TerminalSourceEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.LoginLog;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*免密登入
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Withoutpwdlogin{
	private static WithoutpwdloginBean withoutpwdloginBean;
	private static WithoutpwdloginCall withoutpwdloginCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("免密登入");
	}

	@Before
	public void caseUp() {
		withoutpwdloginBean=new WithoutpwdloginBean();
		withoutpwdloginCall=new WithoutpwdloginCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(withoutpwdloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Withoutpwdlogin_001() {
		Logger.start(true,"免密登入成功");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setTokenSource("app");
			regbean.setRegSource(1);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setClientPort("234");
			withoutpwdloginBean.setSource("App");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(true, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyNotNull(token, "验证AccessToken");
			Logger.verifyEquals(UserId, withoutpwdloginCall.getUserId(), "验证UserId");
			Logger.verifyEquals(regbean.getLoginId(), withoutpwdloginCall.getLoginid(), "验证loginid");
			Logger.verifyNotEquals(token, withoutpwdloginCall.getSign(), "登入会重新生成token");
			Logger.verifyEquals("", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage信息");
			Logger.verifyEquals("0", withoutpwdloginCall.getErrorCode(), "验证ErrorCode信息");
			// Logger.verifyEquals(128, lac.getUserData().getSign().length(),
			// "验证新Token长度");
			// 验证UserInfo表中LastUpdateTime
			Userinfo userinfo = new JUserWapper().selectUserinfoByUid(UserId).get(0);
			Logger.verifyNotNull(userinfo.getLastLoginTime(), "验证UserInfo表中LastUpdateTime");
			// Logger.verifyEquals(false, userinfo.getLastLoginTime().after(new
			// Date()), "验证UserInfo表中LastUpdateTime");
			Thread.sleep(3000);
			// 验证LoginLog表中新增一条记录
			LoginLog loginLog = new JUserWapper().selectLastLoginLogByUid(UserId).get(0);
			Logger.verifyEquals(withoutpwdloginBean.getClientPort(), loginLog.getClientPort(), "验证LoginLog表中getClientPort");
			Logger.verifyEquals(userinfo.getLoginId(), loginLog.getLoginId(), "验证LoginLog表中LoginId");
			Logger.verifyEquals(withoutpwdloginBean.getIp(), loginLog.getClientIp(), "验证LoginLog表中ClientIp");
			// 码头登陆的为0
			Logger.verifyEquals(withoutpwdloginBean.getTerminalSource().getCode(), loginLog.getTerminalSource(), "验证LoginLog表中TerminalSource");
			Logger.verifyEquals(regbean.getDeviceId(), loginLog.getDeviceId(), "验证LoginLog表中DeviceId");
			Logger.verifyNotNull(loginLog.getServerIP(), "验证LoginLog表中ServerIP");

			if (withoutpwdloginCall.getErrorCode().equals("0")) {
				Logger.verifyIsContains("无密登录成功", loginLog.getResponseDesc(), "验证LoginLog表中ResponseDesc");
				Logger.verifyEquals(200, loginLog.getResponseCode(), "验证LoginLog表中ResponseCode");

			} else {
				Logger.verifyEquals(withoutpwdloginCall.getString("lastErrorMessage"), loginLog.getResponseDesc(), "验证LoginLog表中ResponseDesc");
				Logger.verifyEquals(withoutpwdloginCall.getString("ResponseCode"), loginLog.getResponseCode(), "验证LoginLog表中ResponseCode");

			}
			// 风控返回
			// Logger.verifyNotNull(loginLog.getIpArea(), "验证LoginLog表中IpArea");
			Logger.verifyNotNull(loginLog.getCreateTime(), "验证LoginLog表中CreateTime");
			Logger.verifyEquals(regbean.getRegisterType(), loginLog.getUserType(), "验证LoginLog表中UserType");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Withoutpwdlogin_002() {
		Logger.start(true,"不输入moblie，失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
//			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("400", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("mobile不能为空", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Withoutpwdlogin_003() {
		Logger.start(true,"不输入ip，失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
//			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("400", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("ip不能为空", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Withoutpwdlogin_004() {
		Logger.start(true,"不输入source，失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
//			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("400", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("source不能为空", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Withoutpwdlogin_005() {
		Logger.start(true,"不输入tokensource，失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
//			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("400", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("tokenSource不能为空", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Withoutpwdlogin_006() {
		Logger.start(true,"不输入terminalSource，失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
//			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("400", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("terminalSource不能为空", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Withoutpwdlogin_007() {
		Logger.start(true,"source为App时没有deviceId,失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
//			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("5000", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("app登录必须传deviceId", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Withoutpwdlogin_008() {
		Logger.start(true,"用户被锁定时,失败");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			JUserCallService.lockUsers(UserId);
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("6", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("用户被锁定", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Withoutpwdlogin_009() {
		Logger.start(true,"用户不存在时,失败");
		try {
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(TestDataManager.getPhoneNum());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(TestDataManager.getRandomIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
			withoutpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("21", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("用户未找到", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Withoutpwdlogin_010() {
		Logger.start(true,"source为mainsite时没有deviceId,成功");
		try {
			Logger.comment("前置条件准备");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
			JUserCallService.UpdateTradingPwd(UserId, "abc123");
			Date now = new Date();
			Thread.sleep(3000);
			Logger.comment("开始登陆认证");
			withoutpwdloginBean.setMobile(regbean.getLoginMobile());
			withoutpwdloginBean.setSource("app");
			withoutpwdloginBean.setIp(regbean.getClientIp());
			withoutpwdloginBean.setTerminalSource(TerminalSourceEnum.IOS);
			withoutpwdloginBean.setTokenSource("app");
//			withoutpwdloginBean.setDeviceId(regbean.getDeviceId());
			withoutpwdloginCall.setData(withoutpwdloginBean);
			withoutpwdloginCall.callService();

			Logger.verifyEquals(false, withoutpwdloginCall.getSuccess(), "验证Success");
			Logger.verifyEquals("5000", withoutpwdloginCall.getErrorCode(), "验证ErrorCode");
			Logger.verifyIsContains("app登录必须传deviceId", withoutpwdloginCall.getLastErrorMessage(), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}